package com.feihong.txgw.ui.activity.integral;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.feihong.txgw.R;
import com.feihong.txgw.adapter.user.InviteFriendsReAdapter;
import com.feihong.txgw.inter.contract.integral.IFriendContract;
import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.model.bean.invitefriend.InviteFriendsEntity;
import com.feihong.txgw.tx_precenter.InviteFriendsPrecenter;
import com.feihong.txgw.ui.mvp.BaseMVPCompateActivity;
import com.feihong.txgw.ui.mvp.precenter.AbBasePrecenter;

import java.util.List;

import butterknife.BindView;

/**
 * @author wangziheng
 * @name txgw_app
 * @class name：com.feihong.txgw.ui.activity.integral
 * @class describe:  邀请好友
 * @time 2017/8/8 12:29
 * @change
 * @chang time
 * @class describe
 */
public class InviteFriendsActivity extends BaseMVPCompateActivity<IFriendContract.AbFriendMainPrecenter> implements
        IFriendContract.IFriendMainView {
    @BindView(R.id.inviteFriends_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.invite_friends_recyclerView)
    RecyclerView mShowContentLayout;
    /**
     * 暂无邀请的好友
     */
    @BindView(R.id.invite_friends_no_layout)
    LinearLayout mInviteFriendsNullLayout;
    /**
     * 网络错误layout
     */
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectNetErrorLayout;
    @BindView(R.id.re_query_data_layout)
    LinearLayout mRequeryDataBtn;
    @BindView(R.id.lay_loading)
    RelativeLayout mLoadLayout;


    private InviteFriendsReAdapter mReAdapter;
    private LinearLayoutManager linearLayoutManager;

    private boolean isHasData = false;

    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.activity_invite_friends;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setBackButtonFinish();
        setPageTitle("好友列表");
        linearLayoutManager = new LinearLayoutManager(this);
        mShowContentLayout.setLayoutManager(linearLayoutManager);

        mRefreshLayout.setColorSchemeColors(Color.rgb(223, 47, 47));
        mRefreshLayout.setVisibility(View.GONE);
        mConnectNetErrorLayout.setVisibility(View.GONE);
        mInviteFriendsNullLayout.setVisibility(View.GONE);
        mRequeryDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mConnectNetErrorLayout.setVisibility(View.GONE);
                mLoadLayout.setVisibility(View.VISIBLE);
                getData();
            }
        });
    }

    @Override
    public void bindData() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isHasData = false;
                mPresenter.loadFriendsInfoFirst(InviteFriendsActivity.class.getName());
            }
        });
        mLoadLayout.setVisibility(View.VISIBLE);
        getData();
        initAdapter();
    }

    private void initAdapter() {
        mReAdapter = new InviteFriendsReAdapter(R.layout.invite_friends_item_layout);
        mShowContentLayout.setAdapter(mReAdapter);
    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    /**
     * 列表信息
     *
     * @param list
     */
    @Override
    public void setFriendsInfo(List<InviteFriendsEntity> list) {
        mInviteFriendsNullLayout.setVisibility(View.GONE);
        mRefreshLayout.setVisibility(View.VISIBLE);
        mConnectNetErrorLayout.setVisibility(View.GONE);
        if (isHasData) {
            mReAdapter.addData(list);
        } else {
            mReAdapter.setNewData(list);
            isHasData = true;
        }
        mReAdapter.loadMoreComplete();
    }

    /**
     * 无数据
     */
    @Override
    public void notData() {
        mInviteFriendsNullLayout.setVisibility(View.VISIBLE);
        mRefreshLayout.setVisibility(View.GONE);
        mConnectNetErrorLayout.setVisibility(View.GONE);
    }

    /**
     * 其他错误
     *
     * @param errorMsg
     */
    @Override
    public void otherErrorMsg(String errorMsg) {
        mReAdapter.loadMoreFail();
        if (!isHasData) {
            mInviteFriendsNullLayout.setVisibility(View.VISIBLE);
            mRefreshLayout.setVisibility(View.GONE);
            mConnectNetErrorLayout.setVisibility(View.GONE);
        }
        showSystemToast(errorMsg);
    }

    @Override
    public void loadDataComplated() {
        finishRefresh();
    }

    /**
     * 加载错误
     */
    @Override
    public void loadDataError() {
        mReAdapter.loadMoreFail();
        finishRefresh();
        if (!isHasData) {
            mConnectNetErrorLayout.setVisibility(View.VISIBLE);
            mRefreshLayout.setVisibility(View.GONE);
            mInviteFriendsNullLayout.setVisibility(View.GONE);
        }
        showSystemToast(getString(R.string.connect_service_error));
    }

    @NonNull
    @Override
    public AbBasePrecenter initPresenter() {
        return InviteFriendsPrecenter.create();
    }

    private void finishRefresh() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }

        if(mLoadLayout.getVisibility() == View.VISIBLE){
            mLoadLayout.setVisibility(View.GONE);
        }
    }

    private void getData() {
        mPresenter.loadFriendsInfo(InviteFriendsActivity.class.getName());
    }
}
